//
//  CoreCollectionViewFlowLayout.m
//  objective_c_language
//
//  Created by 王玉龙 on 2018/6/4.
//  Copyright © 2018年 longlong. All rights reserved.
//

#import "CoreCollectionViewFlowLayout.h"

@implementation CoreCollectionViewFlowLayout

/**
 * 当collectionView的显示范围发生改变的时候，是否需要重新刷新布局
 * 刷新的话，会调用下面的方法
 * 1.prepareLayout
 * 2.layoutAttributesForElementsInRect:
 */
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return YES;
}

- (void)prepareLayout
{
    [super prepareLayout];
    
    // 设置水平滚动
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    // 设置内边距
    CGFloat insert = (CGRectGetWidth(self.collectionView.frame) - self.itemSize.width) * 0.5;
    self.sectionInset = UIEdgeInsetsMake(0, insert, 0, insert);
}

- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray<UICollectionViewLayoutAttributes *> *array = [super layoutAttributesForElementsInRect:rect];
    
    float center_X = self.collectionView.contentOffset.x + CGRectGetWidth(self.collectionView.frame) * 0.5;
    
    [array enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
        int scale = 1 - (abs(obj.center.x - center_X) / CGRectGetWidth(self.collectionView.frame));
        
        // 3.2 设置缩放比例
        obj.transform = CGAffineTransformMakeScale(scale, scale);
        
    }];
    
    
    return array;
}
@end
